/*
 * @Author: ZhangXingliang
 * @Date: 2021-11-19 15:24:56
 * @Last Modified by: ZhangXingliang
 * @Last Modified time: 2021-11-19 15:43:28
 */

/**
 * 加权
 *
 * @export
 * @param {ImageData} imageData
 * @return {*}  {ImageData}
 */
export default function gray(imageData: ImageData): ImageData {
  let newImageData = new ImageData(imageData.width, imageData.height)
  for (let i = 0; i < imageData.data.length / 4; i++) {
    let realIndex = i * 4
    newImageData.data[realIndex] = imageData.data[realIndex] * 0.299
    newImageData.data[realIndex + 1] = imageData.data[realIndex + 1] * 0.587
    newImageData.data[realIndex + 2] = imageData.data[realIndex + 2] * 0.114
    newImageData.data[realIndex + 3] = imageData.data[realIndex + 3]
  }
  return newImageData
}

/**
 *
 *
 * @param {ImageData} imageData
 * @return {*}  {ImageData}
 */
function gray2(imageData: ImageData): ImageData {
  let newImageData = new ImageData(imageData.width, imageData.height)
  for (let i = 0; i < imageData.data.length / 4; i++) {
    let realIndex = i * 4
    let gray =
      (imageData.data[realIndex] +
        (imageData.data[realIndex + 1] << 1) +
        imageData.data[realIndex + 2]) >>
      2
    newImageData.data[realIndex] = gray
    newImageData.data[realIndex + 1] = gray
    newImageData.data[realIndex + 2] = gray
    newImageData.data[realIndex + 3] = imageData.data[realIndex + 3]
  }
  return newImageData
}
